package com.reactnativenavigation.controllers;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.easemob.helpdesk.activity.MainActivity;
import com.easemob.helpdesk.utils.CommonUtils;
import com.housekeeperapp.MainApplication;
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.ReactDevPermission;

import java.lang.ref.WeakReference;

public abstract class SplashActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSplashLayout();
        IntentDataHandler.saveIntentData(getIntent());
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!CommonUtils.isAppRunningForeground2(getApplicationContext())){//没有前台运行
            if (!NavigationApplication.instance.isReactContextInitialized()) {
                NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
            }else{
                NavigationApplication.instance.reStartReactContextOnceInBackgroundAndExecuteJS();
            }


        }

    }

    @Override
    protected void onResume() {
        super.onResume();

        if (NavigationApplication.instance.getReactGateway().hasStartedCreatingContext()) {
            boolean is_from_click_notification = getIntent().getBooleanExtra("is_from_click_notification",false);
            boolean isNewCreate = ((MainApplication) getApplication()).isNewCreate();
            if (is_from_click_notification && !isNewCreate){//来自点击事件并且在前台
                finish();
            }
        }
        if (ReactDevPermission.shouldAskPermission()) {
            ReactDevPermission.askPermission(this);
        }

        ((MainApplication) getApplication()).setNewCreate(false);
    }

    private void setSplashLayout() {
        final int splashLayout = getSplashLayout();
        if (splashLayout > 0) {
            setContentView(splashLayout);
        } else {
            setContentView(createSplashLayout());
        }
    }

    /**
     * @return xml layout res id
     */
    @LayoutRes
    public int getSplashLayout() {
        return 0;
    }

    /**
     * @return the layout you would like to show while react's js context loads
     */
    public View createSplashLayout() {
        View view = new View(this);
        view.setBackgroundColor(Color.WHITE);
        return view;
    }

}
